package com.yb.xczx.cloud.user.filter;

import com.yb.core.model.WhiteListProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/23 11:20
 * FileName: LoginFilter
 * Description:
 */
@Component
public class LoginFilter implements Filter {

    AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Autowired
    WhiteListProperties whiteListProperties;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {


        this.doFilter((HttpServletRequest) request,(HttpServletResponse) response,chain);
    }

    private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        //如果请求路径在白名单直接放行
//        String requestURI=request.getRequestURI();
//        if(!CollectionUtils.isEmpty(whiteListProperties.getUrls())){
//
//            boolean matched=false;
//
//            for (String url : whiteListProperties.getUrls()) {
//                if(antPathMatcher.match(url,requestURI)){
//                    matched=true;
//                    break;
//                }
//            }
//            if(matched){
//                chain.doFilter(request,response);
//                return;
//            }
//
//            return;
//        }

        chain.doFilter(request,response);
    }
}
